package com.yc.http.tomcat.impl;

import com.yc.http.tomcat.HttpServlet;
import com.yc.http.tomcat.HttpServletRequest;
import com.yc.http.tomcat.HttpServletResponse;
import com.yc.http.tomcat.ServletException;

import java.io.IOException;

public abstract class HttpServletImpl implements HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 分发请求
        if("GET".equals(request.getMethod())){
            doGet(request,response);
        } else if("POST".equals(request.getMethod())){
            doPost(request,response);
        } else if("OPTIONS".equals(request.getMethod())) {
            doOptions(request,response);
        } else {
            // 请自行扩展 。。。
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        throw new ServletException("405 错误！");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        throw new ServletException("405 错误！");
    }

    @Override
    public void doOptions(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        throw new ServletException("405 错误！");
    }

    @Override
    public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        throw new ServletException("405 错误！");
    }

    @Override
    public void doHead(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        throw new ServletException("405 错误！");
    }

    @Override
    public void doTrace(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        throw new ServletException("405 错误！");
    }

    @Override
    public void init() {

    }

    @Override
    public void destroy() {

    }
}
